package proto_wearebatman;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

    /**
     * Írd ide a neved, ha importáltad a protot (és észrevetted ezt :D):
     * Torma Gábor
     * Madarassy Zoltán
     */

    static FileWriter outFile;
    static PrintWriter out;
  
    public static void log(String s) {
        out.println(s);        
    }

    public static void println(String s) {
        out.println(s);
        System.out.println(s);
    }

    public static void println() {
        out.println();
        System.out.println();
    }

    public static void main(String[] args) {
        try{
            outFile = new FileWriter("output.txt");
            out = new PrintWriter(outFile);
        }
        catch(Exception e) {

        }
        BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
        String cmd = null;
        String command = "";
        String param1 = null;
        String param2 = null;
        String param3 = null;
        String param4 = null;
        Circuit c = null;
        do {
            param1 = null;
            param2 = null;
            param3 = null;
            param4 = null;
            System.out.print("\n> ");
            try {
                cmd = kb.readLine();
            } catch (IOException e) {
                Main.println("CommandNotFound");
            }
            Main.log("\n> "+cmd);
            StringTokenizer st = new StringTokenizer(cmd, " ");
            if (st.hasMoreTokens()) {
                command = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                param1 = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                param2 = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                param3 = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                param4 = st.nextToken();
            }

            if (command.equals("help")) {
                Main.println("Listing available commands:");
                Main.println("quit:                       Exit the application");
                Main.println("loadCircuit [test.txt]:     Loads the circuit from test.txt");
                Main.println("tick [x]:                   Ticks the circuit by [x] step.");
                Main.println("startSimulation:            Starts the simulation");
                Main.println("stopSimulation:             Stops the simulation");
                Main.println("listAll:                    Lists all the components with their current states");
                Main.println("list [name]:                Lists one chosen component with it's current state");
                Main.println("setState [name]:            Sets the chosen switch's state");
                Main.println("setGeneratorBits [name]:    Sets the chosen generator's bits");
            } else if (command.equals("tick")) {
                if (c != null) {
                    if (param1 == null) {
                        c.tick(1);
                    } else {
                        c.tick(Integer.parseInt(param1.toString()));
                    }
                } else {
                    Main.println("NoLoadedCircuit");
                }

            } else if (command.equals("loadCircuit")) {
                if (param1 == null) {
                    Main.println("ParameterMismatch");
                } else {
                    c = new Circuit(param1);
                }

            } else if (command.equals("list")) {
                if (c != null) {
                    c.list(param1);
                } else {
                    Main.println("NoLoadedCircuit");
                }

            } else if (command.equals("listAll")) {
                if (c != null) {
                    c.listAll();
                } else {
                    Main.println("NoLoadedCircuit");
                }

            } else if (command.equals("setState")) {
                c.setSwitch(param1, param2);
            } else if (command.equals("setGeneratorBits")) {
                c.setGenerator(param1, param2);

            } else if (command.equals("startSimulation")) {
                if (c != null) {
                    c.setSimulationState(true);
                } else {
                    Main.println("NoLoadedCircuit");
                }

            } else if (command.equals("stopSimulation")) {
                if (c != null) {
                    c.setSimulationState(false);
                } else {
                    Main.println("NoLoadedCircuit");
                }

            } else {
                if (!command.equals("quit")) {
                    Main.println("CommandNotFound");
                }
            }
        } while (!command.equals("quit"));        
        try {
            out.close();
            outFile.close();
        }
        catch(Exception e) {

        }
    }
}
